﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entities;
using System.Data.SqlClient;

namespace SurveyGUI
{
    public partial class ChangePassAdminForm : Form
    {
        IAccountRepository rep = new AccountRepository();

        public ChangePassAdminForm()
        {
            InitializeComponent();
        }

        //bien gan ten admin len txtTenDN cua form ChangPassAdmin
        public string tenUserAdmin
        {
            set { txtTenDN.Text = value; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtMKMoi.Text != "" && txtMKNhapLai.Text != "")
            {
                if (txtMKMoi.Text.CompareTo(txtMKNhapLai.Text) == 0)
                {
                    string conectionString;

                    conectionString = "Data Source=TRUONG-PC\\SQLEXPRESS;Initial Catalog=Nhom15;Integrated Security=True";
                    string query;
                    SqlConnection conn = new SqlConnection(conectionString);
                    try
                    {
                        conn.Open();
                        query = "UPDATE TaiKhoan SET Pass = '" + txtMKMoi.Text + "' WHERE UserName like '" + txtTenDN.Text + "'  ";
                        SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn;
                        sqlCommand.CommandText = query;
                        int i = sqlCommand.ExecuteNonQuery();
                        MessageBox.Show("Đổi mật khẩu thành công!");

                        this.Close();
                    }
                    catch (Exception ex) { MessageBox.Show(ex.StackTrace); }
                    finally
                    { conn.Close(); conn.Dispose(); }
                }
                else
                {
                    MessageBox.Show("Mật khẩu phải trùng nhau!. Nhập lại?", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập thông tin mật khẩu!", "Cảnh báo!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
